package cn.stylefeng.guns.modular.common.service;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.config.redis.redis.Redis0Template;
import cn.stylefeng.guns.constant.Constant;
import cn.stylefeng.guns.modular.info.entity.League;
import cn.stylefeng.guns.modular.info.entity.Team;
import cn.stylefeng.guns.modular.info.mapper.LeagueMapper;
import cn.stylefeng.guns.modular.info.mapper.TeamMapper;
import cn.stylefeng.guns.wechat.WeChatMsgSend;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @创建人 xiesm
 * @创建时间 2021/1/23
 * @描述
 */
@Service
public class CommonService {
    @Autowired(required = false)
    private TeamMapper teamMapper;
    @Autowired(required = false)
    private LeagueMapper leagueMapper;
    @Autowired
    private Redis0Template redis0Template;

    /**
     * @描述 获取球队名称
     * @创建人 xiesm
     * @创建时间 2021/1/23
     */
    public String getTeamName(Integer teamId) {
        String teamName = Constant.teamIdDict.get(teamId);
        if (teamName == null) {
            Team team = teamMapper.selectOne(new QueryWrapper<Team>().eq("id", teamId));
            if (team != null) {
                Constant.teamIdDict.put(team.getId(), team.getName());
                teamName = team.getName();
            }
        }
        return teamName;
    }

    /**
     * @描述 获取联赛名称
     * @创建人 xiesm
     * @创建时间 2021/1/23
     */
    public String getLeagueName(Integer leagueId) {
        String leagueName = Constant.leagues.get(leagueId);
        if (leagueName == null) {
            League league = leagueMapper.selectOne(new QueryWrapper<League>().eq("id", leagueId));
            if (league != null) {
                Constant.leagues.put(league.getId(), league.getName());
                leagueName = league.getName();
            }
        }
        return leagueName;
    }

    public String getWechatToken(int appid) throws IOException {
        ValueOperations valueOperations = redis0Template.opsForValue();
        String key = StrUtil.format("wechat:token:{}", appid);
        String token = String.valueOf(valueOperations.get(key));
        if (token == null || Objects.equals("null", token)) {
            WeChatMsgSend swx = new WeChatMsgSend();
            token = swx.getToken("wwd10c4d16064ab819", getSecret(appid));
            valueOperations.set(key, token, 60, TimeUnit.MINUTES);
        }
        return token;
    }

    public String getSecret(int appid) {
        String secret = null;
        switch (appid) {
            case 1000002:
                //测试
                secret = "eYsa-8jsZShJdEfWKbFNMelxEM3cuLZbw_2Nmv9A4ps";
                break;
            case 1000003:
                //日报
                secret = "NxGvba13Tme8eRR4pfVadHpDfDzCS1zC62gyjKJtKmk";
                break;
            case 1000004:
                //周报
                secret = "Hsc1h_K0237XIbmgT9CiHUJjm1zqDp3GUeiEdk-0pg0";
                break;
            case 1000005:
                //乌龙球
                secret = "ZxZ9-gsMY7_0hq7l_KUo-9mylpItk95xaLriUwVLkTg";
                break;
            case 1000006:
                //红牌及点球
                secret = "xxOC96TalU42gGhBZpLJC82U8iW2YevCO9WN1rBmm3Y";
                break;
            case 1000007:
                //黄牌前换人
                secret = "ZKRds-H4EAz9gKjnOd_tMucd0el6ru9FenPTmEcrnLc";
                break;
            case 1000008:
                //前15分钟进球大于1
                secret = "pXhM4RvAe3eVkiQ20W9uZNTm7uGHRnKlxWC5mgNdRKs";
                break;
            case 1000009:
                //替补进球
                secret = "mHHHUTSD7V4ImS7raxq9mxKETe8HfhdlzN8ongrppLA";
                break;
            case 1000010:
                //前10分种黄牌大于0.5
                secret = "ftAzzuKqvIQNbGcWA-845WH4KNxrzdpp6DNDY9OjddQ";
                break;
            case 1000011:
                //反超
                secret = "0ofAg--Wv25DB4Ao5-QH-ZiPUuGphvFtPWKS-ApjiBo";
                break;
            case 1000012:
                //角球相同
                secret = "zt8I_RNOYn_CAEsUvxN5VkkzkjQq7CRWagvmyftNoZc";
                break;
            case 1000013:
                //半场获取为不同队
                secret = "uFxp-gvW5fGySs9KdsnidMH5SNzfkulC_zj2MVcnbvo";
                break;
            case 1000014:
                //半场获取为不同队
                secret = "1OMfiL-xdWXNO1y2N-Rf8CGIhmpWR21pYyqa4I_HR5U";
                break;
            case 1000015:
                secret = "Kvifm2v8q0NiBe-BEYtkHW1IChgjvNlxTF3lsR7RFPs";
                break;
            case 1000019:
                secret = "lXk6WVvBvXzmiQOvgAzGkQgrn5hpUNT-Ne6doCQntKE";
            default:
                break;
        }
        return secret;
    }

}
